#include<F_system.h>


/*
   printf("ID班次\t机型\t起点站\t终点站\n \
     \t起飞时间\t到达时间\t飞行时间\t\
      \t票价\t载客量\t剩余座位\t是否飞\n");

    printf("%-6d\t%-15s\t%-10s\t%-10s\t\n\
           %02d:%02d\t%02d:%02d\t%-10s\t\n\
           %-6d\t%-8d\t%-8d\t%c\n",\
            node->data.id , node->data.flighttype , node->data.startcity ,node->data.arrivecity,\
            node->data.starttime[0] , node->data.starttime[1],node->data.arrivetime[0] , node->data.arrivetime[1],node->data.flytime,\
            node->data.value,node->data.maxNUM,node->data.leftbuyersa,node->data.whetherfly\
         );
*/

//设定航班
void calculate_flytime(F_data *data) 
{
    int start_total_minutes = data->starttime[0] * 60 + data->starttime[1];
    int arrive_total_minutes = data->arrivetime[0] * 60 + data->arrivetime[1];

    // 计算时间差值
    int time_difference;

    // 跨日情况处理
    if (data->arrivetime[0] < data->starttime[0] ||
        (data->arrivetime[0] == data->starttime[0] && data->arrivetime[1] < data->starttime[1])) {
        // 如果到达时间小于开始时间，说明跨日了
        time_difference = (24 * 60 + arrive_total_minutes) - start_total_minutes;
    } else {
        time_difference = arrive_total_minutes - start_total_minutes;
    }

    // 将时间差值转换为小时和分钟
    int hours = time_difference / 60;
    int minutes = time_difference % 60;

    // 将时间差值格式化为 "HH:MM" 字符串
    sprintf(data->flytime, "%02dh%02dm", hours, minutes);
}


// 菜单信单 -主菜单
const wchar_t *title_flight= L"航班设定菜单";
const wchar_t *option_flight[] ={L"查看当前暂存航班信息",L"班次",L"机型",L"起点站",L"终点站",L"起飞时间",L"到达时间",L"票价",L"载客量",L"保存并退出",L"取消"};


const wchar_t *title_flight_revise= L"航班修改菜单";
const wchar_t *option_flight_revise[] ={L"查看当前航班信息",L"班次",L"机型",L"起点站",L"终点站",L"起飞时间",L"到达时间",L"票价",L"载客量",L"确认修改",L"取消修改"};


int Sys_flight_set(F_list *list)
{   

    
    F_data data={0,"NULL","NULL","NULL",{00,00},{00,00},"0",0,0,0,'n'}; 
    int flag =0;
    while (flag<10)
    {
        /* code */
    calculate_flytime(&data);

    flag=display_menu(title_flight, option_flight, 11);
     switch (flag)
    {   

        case 1:
            system("clear");
            printf("暂存的航班数据：\n");
            printf("班次\t机型\t\t起点站\t终点站\t起飞时间\t到达时间\t飞行时间\t票价\t载客量\t剩余座位\t是否飞\n");
            printf("%d\t%s\t%s\t%s\t%02d:%02d\t\t%02d:%02d\t\t%s\t%d\t%d\t%d\t%c\n",\
                    data.id , data.flighttype , data.startcity ,data.arrivecity,\
                    data.starttime[0] , data.starttime[1],data.arrivetime[0] , data.arrivetime[1],data.flytime,\
                    data.value,data.maxNUM,data.leftbuyersa,data.whetherfly\
                  );
            printf("\r\n回车退出");
            getchar();
            getchar();
            break;
        case 2:
            printf("请输入班次:");
            scanf("%d", &data.id);
            break;
        case 3:
            printf("请输入机型:");
            scanf("%s", data.flighttype);
            break;
        case 4:
            printf("请输入出发站:");
            scanf("%s", data.startcity);
            break;
        case 5:
            printf("请输入到达站:");
            scanf("%s", data.arrivecity);
            break;
        case 6:
            printf("请输入起飞时间:");
            scanf(" %d:%d", &data.starttime[0],&data.starttime[1]);
            break;
        case 7:
            printf("请输入到达时间:");
            scanf(" %d:%d", &data.arrivetime[0],&data.arrivetime[1]);
            break;

        case 8:
            printf("请输入票价:");
            scanf("%d", &data.value);
            break;

        case 9:
            printf("请输入载客量:");
            scanf("%d", &data.maxNUM);
            break;

        case 10:
                D_List_insert(list,data,1);
                D_List_file_print(list);
                // 设定航班 - 保存并退出
                printf("保存并退出\n");
                break;
        default:
                // 设定航班 -取消设定
            printf("取消设定\n");
            break;
    }

    }

    return 0;
}

int Sys_flight_revise(F_list *list)
{
    int id;
    system("clear");
    D_List_print(list);
    printf("请输入拟修改的航班班次\r\n");
    scanf("%d",&id);
    if(0==D_List_find_id(list, id))
    {   
        ERRLOG("未找到");
        printf("\n\n未找到班次为 %d 的航班,回车以继续",id);
        getchar();
        getchar();
        return 0;
    }
    F_data data=D_List_find_id(list, id)->data; 
    int flag =0;
    while (flag<10)
    {
        /* code */
    calculate_flytime(&data);

    flag=display_menu(title_flight_revise, option_flight, 11);
     switch (flag)
    {   
        case 1:
            system("clear");
            printf("暂存的航班数据：\n");
            printf("班次\t机型\t\t起点站\t终点站\t起飞时间\t到达时间\t飞行时间\t票价\t载客量\t剩余座位\t是否飞\n");
            printf("%d\t%s\t%s\t%s\t%02d:%02d\t\t%02d:%02d\t\t%s\t\t%d\t%d\t%d\t\t%c\n",\
                    data.id , data.flighttype , data.startcity ,data.arrivecity,\
                    data.starttime[0] , data.starttime[1],data.arrivetime[0] , data.arrivetime[1],data.flytime,\
                    data.value,data.maxNUM,data.leftbuyersa,data.whetherfly\
                  );
            printf("\r\n回车退出");
            getchar();
            getchar();
            break;
        case 2:
            printf("请输入班次:");
            scanf("%d", &data.id);
            break;
        case 3:
            printf("请输入机型:");
            scanf("%s", data.flighttype);
            break;
        case 4:
            printf("请输入出发站:");
            scanf("%s", data.startcity);
            break;
        case 5:
            printf("请输入到达站:");
            scanf("%s", data.arrivecity);
            break;
        case 6:
            printf("请输入起飞时间:");
            scanf(" %d:%d", &data.starttime[0],&data.starttime[1]);
            break;
        case 7:
            printf("请输入到达时间:");
            scanf(" %d:%d", &data.arrivetime[0],&data.arrivetime[1]);
            break;

        case 8:
            printf("请输入票价:");
            scanf("%d", &data.value);
            break;

        case 9:
            printf("请输入载客量:");
            scanf("%d", &data.maxNUM);
            break;
        
        case 10:
                D_List_insert(list,data,1);
                D_List_file_print(list);
                // 设定航班 - 保存并退出
                printf("保存并退出\n");
                break;
        default:
                // 设定航班 -取消设定
            printf("取消设定\n");
            break;
    }

    }

    return 0;






}

/*
//修改航班
int Sys_flight_revise(F_list *list)
{
    int id;
   // char name;

    printf("输入航班的ID进行查找\r\n");
    scanf("%d",&id);
    
   if(EOF== D_list_up_data(list,D_List_find_id(list, id))) 
   {
     printf("没有找到ID为 %d的航班\r\n", id);
     return EOF;
   }
   D_List_file_print(list);
    return 0;
}
*/
//修改用户
int Sys_user_revise(U_list *list,FlightSystem Flight_sys)
{   /*
    #define ADMIN_FILE "data/admin_users.txt"           // 管理员账户信息文件
    #define REGULAR_FILE "data/regular_users.txt"       // 普通用户信息文件
    #define SILVER_FILE "data/silver_members.txt"       // 银卡会员信息文件(9折) 余额 10000
    #define GOLD_FILE "data/gold_members.txt"           // 金卡会员信息文件(8折) 余额 15000
    #define PLATINUM_FILE "data/platinum_members.txt"   // 白金会员信息文件(7折) 余额 20000
    list = S_List_merge(list, Flight_sys.ADMIN_USER);    // 管理员账户信息文件
    list = S_List_merge(list, Flight_sys.REGULAR_USER);  // 普通用户信息文件
    list = S_List_merge(list, Flight_sys.SILVER_USER);   // 银卡会员信息文件 (9折)
    list = S_List_merge(list, Flight_sys.GOLD_USER);     // 金卡会员信息文件 (8折)
    list = S_List_merge(list, Flight_sys.PLATINUM_USER); // 白金会员信息文件 (7折)
*/
   // 开始登录
   // U_data log_data = Log_in(list);
    int grade_flag=0;
    char name[32];
    printf("输入用户的姓名进行查找\r\n");
    scanf("%s",name);
    if(NULL!= S_List_find_name(Flight_sys.REGULAR_USER,name))
    grade_flag=1;
    if(NULL!= S_List_find_name(Flight_sys.SILVER_USER,name))
    grade_flag=2;
    if(NULL!= S_List_find_name(Flight_sys.GOLD_USER,name))
    grade_flag=3;
    if(NULL!= S_List_find_name(Flight_sys.PLATINUM_USER,name))
    grade_flag=4;
   
   
    if(NULL!= S_List_find_name(Flight_sys.REGULAR_USER,name))
    {

        printf("普通用户\r\n");
        S_list_up_data(Flight_sys.REGULAR_USER,S_List_find_name(Flight_sys.REGULAR_USER,name));

        S_List_File_print(Flight_sys.REGULAR_USER,REGULAR_FILE);
    }

    if(NULL!= S_List_find_name(Flight_sys.SILVER_USER,name))
    {
    
        printf("银卡会员1\r\n");

        S_list_up_data(Flight_sys.SILVER_USER,S_List_find_name(Flight_sys.SILVER_USER,name));
        S_List_File_print(Flight_sys.SILVER_USER,SILVER_FILE);
    }

    if(NULL!= S_List_find_name(Flight_sys.GOLD_USER,name))
    {

    printf("金卡会员\r\n");
    S_list_up_data(Flight_sys.GOLD_USER,S_List_find_name(Flight_sys.GOLD_USER,name));
    S_List_File_print(Flight_sys.GOLD_USER,GOLD_FILE);
    }
    if(NULL!= S_List_find_name(Flight_sys.PLATINUM_USER,name))
    {

    printf("白金会员\r\n");
    S_list_up_data(Flight_sys.PLATINUM_USER,S_List_find_name(Flight_sys.PLATINUM_USER,name));
    S_List_File_print(Flight_sys.PLATINUM_USER,PLATINUM_FILE);
    }
    

    return 0;
} 


int Sys_admin_add(U_list *list,FlightSystem Flight_sys)
{


    U_data data;
    printf("增加姓名");
    scanf("%s",data.name);
    printf("增加密码");
    scanf("%s",data.passwd);
    data.grade=1;
    data.value=0;
    memset(data.ticket,0,sizeof(data.ticket));

    S_List_insert(Flight_sys.ADMIN_USER,data,1);
    S_List_File_print(Flight_sys.ADMIN_USER,ADMIN_FILE);
    return 0;

}

// printf("没有找到姓名为 %s的用户\r\n", name);
    // return EOF;
//  S_List_File_print(list,ADMIN_FILE);
/*  查找用户表
U_node *S_List_find_name(U_list *list, char *name);
*/

